package com.coszero.utils.base

import android.content.Context
import android.util.SparseArray
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/**
 * @author xmqian
 * ClassName: BaseRecyclerViewHolder
 *
 *
 * Author:oubowu
 *
 *
 * Fuction: RecyclerView通用适配器Holder
 *
 *
 * CreateDate:2016/2/16 23:00
 *
 *
 * UpdateUser:
 *
 *
 * UpdateDate:
 *
 *
 * @version 1
 */
class BaseRecyclerViewHolder(//上下文对象
    protected var mContext: Context, itemView: View
) :
    RecyclerView.ViewHolder(itemView) {
    //集合类，layout里包含的View,以view的id作为key，value是view对象
    protected var mViews: SparseArray<View?> =
        SparseArray()

    private fun <T : View?> findViewById(viewId: Int): T? {
        var view = mViews[viewId]
        if (view == null) {
            view = itemView.findViewById(viewId)
            mViews.put(viewId, view)
        }
        return view as T?
    }

    fun getView(viewId: Int): View {
        return findViewById<View>(viewId)!!
    }

    fun getTextView(viewId: Int): TextView {
        return getView(viewId) as TextView
    }

    fun getButton(viewId: Int): Button {
        return getView(viewId) as Button
    }

    fun getImageView(viewId: Int): ImageView {
        return getView(viewId) as ImageView
    }

    fun getImageButton(viewId: Int): ImageButton {
        return getView(viewId) as ImageButton
    }

    fun getEditText(viewId: Int): EditText {
        return getView(viewId) as EditText
    }

    fun setText(viewId: Int, value: String?): BaseRecyclerViewHolder {
        val view = findViewById<TextView>(viewId)!!
        view.text = value
        return this
    }

    fun setBackground(viewId: Int, resId: Int): BaseRecyclerViewHolder {
        val view = findViewById<View>(viewId)!!
        view.setBackgroundResource(resId)
        return this
    }

    fun setClickListener(viewId: Int, listener: View.OnClickListener?): BaseRecyclerViewHolder {
        val view = findViewById<View>(viewId)!!
        view.setOnClickListener(listener)
        return this
    }
}
